package com.clickpaas.governmentcloud.common;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.List;

@JsonInclude(JsonInclude.Include.NON_NULL)
@ApiModel(value = "返回包装类", description = "1", reference = "2",discriminator = "3")
public class ServerResponse<T> implements Serializable {

    // 定义jackson对象
    private static final ObjectMapper MAPPER = new ObjectMapper();
    @ApiModelProperty(value = "状态码", example = "状态码", notes = "notes")
    private int code;
    
    @ApiModelProperty(value = "描述", example = "描述", notes = "notes")
    private String message;
    
    @ApiModelProperty(value = "对象", example = "对象", notes = "notes")
    private T data;

    private ServerResponse(int code){
        this.code = code;
    }
    private ServerResponse(int code, String message){
        this.code = code;
        this.message =message;
    }
    private ServerResponse(int code, T data){
        this.code = code;
        this.data = data;
    }
    private ServerResponse(int code, String message, T data ){
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public int getcode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }

    @JsonIgnore
    public boolean isSuccess(){
       return  this.code == ResultStatusEnum.SUCCESS.getCode();
    }

    public static<T> ServerResponse<T> createSuccess(){
        return  new ServerResponse<T>(ResultStatusEnum.SUCCESS.getCode(), ResultStatusEnum.SUCCESS.getProductDescription());
    }
    public static<T> ServerResponse<T> createSuccess(String message){
        return  new ServerResponse<T>(ResultStatusEnum.SUCCESS.getCode(), message);
    }
    public static<T> ServerResponse<T> createSuccess(T data ){
        return  new ServerResponse<T>(ResultStatusEnum.SUCCESS.getCode(), ResultStatusEnum.SUCCESS.getProductDescription(), data);
    }
    public static<T> ServerResponse<T> createSuccess(String message, T data){
        return  new ServerResponse<T>(ResultStatusEnum.SUCCESS.getCode(), message, data);
    }
    public static<T> ServerResponse<T> createSuccess(int code,String message, T data){
        return  new ServerResponse<T>(code, message, data);
    }


    public static<T> ServerResponse<T> createError(){
        return  new ServerResponse<T>(ResultStatusEnum.ERROR.getCode(), ResultStatusEnum.ERROR.getProductDescription());
    }
    public static<T> ServerResponse<T> createError(String message){
        return  new ServerResponse<T>(ResultStatusEnum.ERROR.getCode(), message);
    }
    public static<T> ServerResponse<T> createError(String message, T data){
        return  new ServerResponse<T>(ResultStatusEnum.ERROR.getCode(), message,data);
    }
    public static<T> ServerResponse<T> createError(int code,String message){
        return  new ServerResponse<T>(code, message);
    }
    public static<T> ServerResponse<T> createError(int code,String message, T data){
        return  new ServerResponse<T>(code, message,data);
    }

    /**
     * 将json结果集转化为ServerResponse对象
     *
     * @param jsonData json数据
     * @param clazz ServerResponse中的object类型
     * @return
     */
    public static ServerResponse formatToPojo(String jsonData, Class<?> clazz) {
        try {
            if (clazz == null) {
                return MAPPER.readValue(jsonData, ServerResponse.class);
            }
            JsonNode jsonNode = MAPPER.readTree(jsonData);
            JsonNode data = jsonNode.get("data");
            Object obj = null;
            if (clazz != null) {
                if (data.isObject()) {
                    obj = MAPPER.readValue(data.traverse(), clazz);
                } else if (data.isTextual()) {
                    obj = MAPPER.readValue(data.asText(), clazz);
                }
            }
            return new ServerResponse(jsonNode.get("code").intValue(), jsonNode.get("message").asText(), obj);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 没有object对象的转化
     *
     * @param json
     * @return
     */
    public static ServerResponse format(String json) {
        try {
            return MAPPER.readValue(json, ServerResponse.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Object是集合转化
     *
     * @param jsonData json数据
     * @param clazz 集合中的类型
     * @return
     */
    public static ServerResponse formatToList(String jsonData, Class<?> clazz) {
        try {
            JsonNode jsonNode = MAPPER.readTree(jsonData);
            JsonNode data = jsonNode.get("data");
            Object obj = null;
            if (data.isArray() && data.size() > 0) {
                obj = MAPPER.readValue(data.traverse(),
                        MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));
            }
            return new ServerResponse(jsonNode.get("code").intValue(), jsonNode.get("message").asText(), obj);
        } catch (Exception e) {
            return null;
        }
    }


}
